﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using Microsoft.Extensions.Configuration;
using Simple.Repository;
using Simple.Tools;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Web.Common.autofac
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
    public static class AutofacRegisterExtension
    {
        /// <summary>
        /// 注册数据库和业务模块
        /// </summary>
        /// <param name="containerBuilder"></param>
        public static void RegisterDateBaseService(this ContainerBuilder containerBuilder)
        {

            string rootPath = AppDomain.CurrentDomain.BaseDirectory;
            string resDllPath = Path.Combine(rootPath, "Simple.Repository.dll");
            string iResDllPath = Path.Combine(rootPath, "Simple.IRepository.dll");
            string interfaceDllPath = Path.Combine(rootPath, "Simple.Service.dll");
            string serviceDllPath = Path.Combine(rootPath, "Simple.Service.dll");
            Assembly assemblyRepository = Assembly.LoadFrom(resDllPath);
            Assembly assemblyIRepository = Assembly.LoadFrom(iResDllPath);
            Assembly assemblyService = Assembly.LoadFrom(serviceDllPath);
            Assembly assemblyIterface = Assembly.LoadFrom(interfaceDllPath);


            string connectionStr = App.Configuration.GetValue<string>("ConnectionStrings:MySqlConString");


            List<ConnectionConfig> connectionConfigs = new List<ConnectionConfig>();


            ConnectionConfig connectionConfig = new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = connectionStr,
                ConfigId = "Default",
                IsAutoCloseConnection = true,
            };

            ISqlSugarClient dbClient = new SqlSugarClient(connectionConfig);




            containerBuilder.RegisterInstance(connectionConfig).SingleInstance().PropertiesAutowired(new AutowiredPropertySelector());

            containerBuilder.RegisterType<SqlSugarClient>().As<ISqlSugarClient>().InstancePerLifetimeScope().PropertiesAutowired(new AutowiredPropertySelector());

            #region 标记接口支持aop
            containerBuilder.RegisterAssemblyTypes(assemblyRepository, assemblyIRepository).
                AsImplementedInterfaces().PropertiesAutowired(new AutowiredPropertySelector()).
                EnableInterfaceInterceptors();

            containerBuilder.RegisterAssemblyTypes(assemblyService, assemblyIterface).
                PropertiesAutowired(new AutowiredPropertySelector()).AsImplementedInterfaces()
                .EnableInterfaceInterceptors();

            #endregion 标记接口支持aop



        }
        /// <summary>
        /// 注册工具服务模块
        /// </summary>
        /// <param name="containerBuilder"></param>
        public static void RegisterToolService(this ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterType<SimpleMapper>().As<ISimpleMapper>().SingleInstance();
        }
    }
}
